const mysql = require('mysql')
var setting = require('../config/config');

// 连接池扩展封装
var poolextend = function (target, source, flag) {
    for (var key in source) {
        if (source.hasOwnProperty(key)) {
            flag ? (target[key] = source[key]) : (target[key] === void 0 && (target[key] = source[key]));
        }
    }
    return target;
}

class MysqlPool {
    constructor() {
        this.flag = true;
        this.pool = mysql.createPool(poolextend({}, setting.mysql));
    }
    getPool() {
        if (this.flag) {
            this.pool.on('connection', (connection) => {
                connection.query('SET SESSION auto_increment_increment=1');
                this.flag = false;
            });
        }
        return this.pool;
    }
}

module.exports = MysqlPool;